<?php

class Mproducts extends TST_Model {

    protected $table_products = "products";
    protected $table_category = "category";
    protected $selected_columns = "*";
    protected $selected_columns_cart = "id,product_price,product_name,product_code";

    function Mproducts() {
        parent::TST_Model();
    }
    
    public function getProductDetailByProductUrl($product_url, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT p.id, p.name, p.friendly_url, p.price, p.full_description
                FROM {$this->table_products} p
                WHERE p.status = ?
                AND   p.friendly_url = ?     
                ";
        return $this->query_row($sql, array($status, $product_url));
    }

    public function getFirstProductOfCategory($category_url, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT p.id, p.name, p.category_id, p.friendly_url
                FROM {$this->table_products} p INNER JOIN {$this->table_category} c ON p.category_id = c.id
                WHERE p.status = ?
                AND   c.friendly_url = ?     
                ";
        return $this->query_row($sql, array($status, $category_url));
    }

    public function getAllProductsByCategoryURL($category_url, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT p.id, p.name, p.friendly_url, p.price, p.icon_image
                FROM {$this->table_products} p INNER JOIN {$this->table_category} c ON p.category_id = c.id
                WHERE p.status = ?
                AND   c.friendly_url = ?     
                ";
        return $this->query_array($sql, array($status, $category_url));
    }

    public function IdentifyName($name, $status = "active") {
        $sql = "SELECT name
                    FROM {$this->table_product}
                    WHERE name = ?
                    AND status = ?
                ";
        return $this->query_row($sql, array($name, $status));
    }

    public function getProductAllForCategoriesID($language, $id, $status) {
        $sql = "SELECT *                                    
                    FROM {$this->table_product}
                    WHERE language = ? 
                    AND   status = ?
                    AND   category_id = ?
                    ORDER BY in_order, name ";
        return $this->query_array($sql, array($language, $status, $id));
    }

    public function getProductByCategoryIDAndURL($language, $id, $product_url, $status) {
        $sql = "SELECT {$this->table_product}.*                                    
                    FROM {$this->table_product},{$this->table_categories}
                    WHERE {$this->table_product}.language = ? 
                    AND   {$this->table_product}.status = ?
                    AND   {$this->table_product}.category_id = {$this->table_categories}.id
                    AND   {$this->table_categories}.friendly_url = ?
                    AND   {$this->table_product}.friendly_url = ?
                    ORDER BY {$this->table_product}.in_order, {$this->table_product}.name ";
        return $this->query_row($sql, array($language, $status, $id, $product_url));
    }

    public function getProductByID($language, $id, $status) {
        $sql = "SELECT *                                    
                    FROM {$this->table_product}
                    WHERE language = ?                     
                    AND   status = ?                   
                    AND   id = ?
                    ORDER BY in_order, name ";
        return $this->query_row($sql, array($language, $status, $id));
    }

    //for Admin -------------------------------------------------------------------------------------------
    public function getProductALLAd($language, $status) {
        $sql = "SELECT DISTINCT {$this->table_product}.*                                   
                    FROM {$this->table_product},{$this->table_categories}
                    WHERE {$this->table_product}.language = ? 
                    AND   {$this->table_product}.category_id = {$this->table_categories}.id
                    AND   {$this->table_categories}.status = 'active'
                    AND   {$this->table_product}.status = ?
                    ORDER BY {$this->table_product}.in_order,{$this->table_product}.name ";
        return $this->query_array($sql, array($language, $status));
    }

    public function getProductALLAd1($language, $status) {
        $sql = "SELECT DISTINCT {$this->table_product}.name, {$this->table_product}.id                                 
                    FROM {$this->table_product},{$this->table_categories}
                    WHERE {$this->table_product}.language = ? 
                    AND   {$this->table_product}.category_id = {$this->table_categories}.id
                    AND   {$this->table_categories}.status = 'active'
                    AND   {$this->table_product}.status = ?
                    ORDER BY {$this->table_product}.in_order,{$this->table_product}.name ";
        return $this->query_array($sql, array($language, $status));
    }

    public function getProductAllForCategoriesIDAd($language, $id) {
        $sql = "SELECT *                                    
                    FROM {$this->table_product}
                    WHERE language = ? 
                    AND   status NOT LIKE 'delete'
                    AND   category_id = ?
                    ORDER BY in_order, name ";
        return $this->query_array($sql, array($language, $id));
    }

    public function update($id, $data) {
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_product, $data, $where);
        return $this->db->query($sql);
    }

    public function addNewProduct($data) {
        $this->db->insert($this->table_product, $data);
        return $this->db->insert_id();
    }

    public function delete($id) {
        $data = array(
            'status' => 'delete'
        );
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_product, $data, $where);
        return $this->db->query($sql);
    }

}
